use hypers::prelude::*;
use hypers::tracing::info;

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt().init();
    println!("current_dir: {:?}", std::env::current_dir().unwrap());
    let base_dir = std::env::current_dir()
        .unwrap()
        .join("compression/static")
        .canonicalize()?;
    println!("Base Dir: {base_dir:?}");

    /*
        http://127.0.0.1:7878/
        http://127.0.0.1:7878/ws_chat
        http://127.0.0.1:7878/sse_chat
        http://127.0.0.1:7878/todos
     */
    let mut root = Router::default();
    root.get("/ws_chat", StaticFile::new(base_dir.join("ws_chat.txt")));
    root.get("/sse_chat", StaticFile::new(base_dir.join("sse_chat.txt")));
    root.get("/todos", StaticFile::new(base_dir.join("todos.txt")));
    root.get("/*", StaticDir::new(base_dir).listing(true));

    root.hook(
        Compression::new().force_priority(true),
        vec!["/ws_chat"],
        None,
    );
    root.hook(
        Compression::new().enable_brotli(CompressionLevel::Fastest),
        vec!["/sse_chat"],
        None,
    );
    root.hook(
        Compression::new().enable_zstd(CompressionLevel::Fastest),
        vec!["/todos"],
        None,
    );
    root.hook(
        Compression::new().enable_gzip(CompressionLevel::Fastest),
        vec!["/*"],
        None,
    );

    info!("root = {:#?}", root);
    let listener = hypers::TcpListener::bind("127.0.0.1:7878").await?;
    hypers::listen(root, listener).await
}
